package com.xb.loan.cashier.biz.job;

import com.xb.loan.cashier.biz.dao.bean.CashierChargeOrder;
import com.xb.loan.cashier.biz.service.CashierChargeOrderService;
import com.xb.loan.cashier.biz.task.TaskManagerFactory;
import com.xb.loan.cashier.biz.task.plat.PushChargeTask;
import com.xb.loan.cashier.biz.util.Configure;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 监控充值回推定时任务
 */
public class PlatPushChargeJob extends AbstractCronTask {
    
    private static final Logger log = LoggerFactory.getLogger(PlatPushChargeJob.class);
    
    private CashierChargeOrderService cashierChargeOrderService;
    
    /**
     * 1.分页查询N分钟前未回推充值数据，如果未找到直接退出
     * 2.启动一个回推任务
     *
     *
     */
    @Override
    public void run() {
        cashierChargeOrderService = getSpringBean("cashierChargeOrderService");
	    log.info("run 开始查询未回推充值订单");
	    int minute = Configure.getIntValue("job.platpushcharge.before.minute");
	    int limit = Configure.getIntValue("job.platpushcharge.pagesize");
	    int start = 0;
	    int pageNum = 0;
	    while (true) {
	        List<CashierChargeOrder> list = cashierChargeOrderService.queryByPushStatus(minute,limit,start);
	        if (list==null || list.size()==0) {
	            log.info("run 没找到未推送的订单强制退出!");
	            break;
	        }
	        log.info("run 未回推总条数："+list.size());
	        if (pageNum>0) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                   log.error(e.getMessage(),e);
                }
            }
	        for (CashierChargeOrder cashierChargeOrder : list) {
	            PushChargeTask pct = getSpringBean("pushChargeTask");
                pct.setCashierChargeOrder(cashierChargeOrder);
                
                //把推送充值结果放到线程池中推送
                TaskManagerFactory.getTaskManager(ICashierEnumConst.TaskManagerCode.PUSH_CHARGE_RESULT).addTask(pct);
	        }
	        start = (++pageNum)*limit;
        }
	}
    
}
